﻿var products;


function refreshImage() {

    //image refreshing the in the create view with using object from image.js 

    $("#factorycreateimage").html("");

    var selectedFactoryName = $("#factoryName").val();

    if (typeof createImages[selectedFactoryName] != "undefined") {
        var newImage = $("<img/>");
        newImage.attr("src", createImages[selectedFactoryName]);

        $("#factorycreateimage").append(newImage);
    }
}

//getting from server the selected product quantity from the warehouse
function computeProductCapacityAndRefresh() {

    var selectedProductIndex = $("#product").val();

    var selectedProductId = products[selectedProductIndex].Id;

    $.post("getProductsAvailabelity", { productId: selectedProductId },
    function (response) {


        $(".capacity").first().html(response + " pieces");

    });
}



$(document).ready(function () {

    //when we filled the factory name, we can 

    $("#factoryName").blur(function () {


        $.post("getProductsForFactory", { factoryName: $(this).val() }, function (data) {
            console.log(data);
            if (data.length > 0) {
                refreshImage();
                products = data;

                var productList = $("#product");
                productList.html("");
                for (var i = 0; i < data.length; i++) {
                    var newProduct = $("<option></option>");
                    newProduct.html(data[i].Name);
                    newProduct.attr("value", i);

                    if (i == 0) {
                        newProduct.prop("selected", true);
                    }

                    productList.append(newProduct);
                }



                computeProductCapacityAndRefresh();
            }
            else {
                $("#factorycreateimage").html("");
                delete products;
                var productList = $("#product");
                productList.html("");
                var newProduct = $("<option></option>");
                newProduct.html("There is no available product.");
                productList.append(newProduct);

                $(".pricevalue").first().html("");
                $(".capacity").first().html("");
                $("#quantity").val("");
                $("#factoryName").val("");
            }

        });

    });


    $("#product").change(function () {
        computeProductCapacityAndRefresh();
    });


    $("#quantity").blur(function () {

        if (!isNaN($(this).val())) {

            var selectedProductId = $("#product").val();
            var price = products[selectedProductId].Price;

            var newPrice = price * $(this).val();
            $(".pricevalue").first().html(newPrice);
        }
    });

    $("#orderButton").click(function () {
        var selectedProductId = $("#product").val();
        var quantityValue = $("#quantity").val();


        $.post("addOrder", {
            selectedProduct: products[selectedProductId].Id,
            quantity: quantityValue
        }, function () {

            delete products;
            var productList = $("#product");
            productList.html("");
            var newProduct = $("<option></option>");
            newProduct.html("There is no available product.");
            productList.append(newProduct);

            $(".pricevalue").first().html("");
            $("#quantity").val("");
            $("#factoryName").val("");
            $(".capacity").first().html("");

        });
    });
});


function getPrice() {
    return 5;
}
function getDatas() {
    factoryName = document.getElementById("factoryName");
    if (factoryName.value == "") {
        alert("You must give the name of the factory");
        return;
    }
    if (factoryName.value.search(/\d/i) != -1) {
        alert("Do not give number to name");
        factoryName.value = "";
        return;
    }
    factoryName.value = "";
}
function order() {
    if (factoryName.value == "") {
        alert("You must give the name of the factory");
        return;
    }
    if (factoryName.value.search(/\d/i) != -1) {
        alert("Do not give number to name");
        factoryName.value = "";
        return;
    }
    quantity = document.getElementById("quantity");

    if (quantity.value == "") {
        alert("You must give the quantity");
        return;
    }
    factoryName.value = "";
    quantity.value = "";
    factoryName.value = "";
    document.getElementById("price").value = "";
    document.getElementById("warehouse").value = "";
    document.getElementById("factory").value = "";
    alert("Your oder is OK");
}